//
//  NewsDetailView.m
//  MusicNews
//
//  Created by lanou3g on 15/10/15.
//  Copyright © 2015年 LC.J. All rights reserved.
//

#import "NewsDetailView.h"
#import "DetailModel.h"
@implementation NewsDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.detailModel = [[DetailModel alloc] init];
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _webView.opaque = NO;
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.backgroundColor = [UIColor whiteColor];
        _title = [[UILabel alloc] init];
        _title.numberOfLines = 0;
        [_webView.scrollView addSubview:_title];
        _source = [[UILabel alloc] init];
        [_webView.scrollView addSubview:_source];
        [self addSubview:_webView];
    }
    return self;
}


- (void)setDetailModel:(DetailModel *)detailModel
{
    
    _detailModel = detailModel;
    [self.webView loadHTMLString:_detailModel.bodyString baseURL:nil];
    CGFloat height = [self heightForString:_detailModel.title width:self.frame.size.width - 20 size:20];
    self.title.text = detailModel.title;
    self.title.frame = CGRectMake(10, -height - 35, 355, height);
    self.title.font = [UIFont systemFontOfSize:20];
    [_webView.scrollView addSubview:_title];
    
    self.source.text = [NSString stringWithFormat:@"%@  %@", _detailModel.source, [_detailModel.ptime substringWithRange:NSMakeRange(5, 11)]];
    self.source.frame = CGRectMake(10, - 35, self.frame.size.width - 20, 25);
    self.source.font = [UIFont systemFontOfSize:14];
    [_webView.scrollView addSubview:_source];
    _webView.scrollView.contentInset = UIEdgeInsetsMake(height + 50 + 64, 0, 66, 0);
}

- (CGFloat)heightForString:(NSString *)string width:(CGFloat)width size:(NSInteger)size
{
    NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:size]};
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.height;
}


@end
